package com.remon.quartz.jpush;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class JpushBulider {

    @Autowired
    private JPushClient jPushClient;

    /**
     * 构建payload 数据
     *
     * @param alias2tag
     * @param msg
     * @param params
     * @return
     */
    public PushPayload buildPayload(String alias2tag, String msg, Map<String, String> params) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag(alias2tag))
//                        .addAudienceTarget(AudienceTarget.alias(alias2tag))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(msg)
                        .addExtras(params)
                        .build())
                .setNotification(Notification.alert(msg))
//                .setNotification(Notification.ios(msg, params))
                .build();
    }

    /**
     * 推送消息数据
     *
     * @param alias2tag
     * @param msg
     * @param params
     */
    public void send(String alias2tag, String msg, Map<String, String> params) {
        try {
            System.out.println("params@@@" + JSON.toJSONString(params));
            PushResult result = jPushClient.sendPush(buildPayload(alias2tag, msg, params));
            System.out.println("+++++++++++++++++++消息推送中+++++++++++++++++++");
            System.out.println(result.toString());
            System.out.println("++++++++++++++++++++++++++++++++++++++");
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }


    /**
     * 推送消息数据
     *
     * @param alias2tag
     * @param msg
     * @param paramJson
     */
    public void send(String alias2tag, String msg, String paramJson) {
        final Map mapObj = JSONObject.parseObject(paramJson, Map.class);
        send(alias2tag, msg, mapObj);
    }

    /**
     * 发送广播
     *
     * @param msg
     */
    public PushResult sendAll(String msg) {
        try {
            PushResult result = jPushClient.sendPush(PushPayload.messageAll(msg));
            System.out.println(result);
            return result;
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }


}
